﻿using System;
using System.Collections.Generic;
using System.Text;

namespace game2
{
    public enum elements { e = 0, x = 1, o = 2, outSideMap = 3 };

    public class Core
    {
        Player p;
        Computer c;

        public bool c_win = false;
        public bool p_win = false;

        

        public Core() 
        {
            p = new Player(this);
            c = new Computer(this);
        }


        public elements[,] map = new elements[3, 3];

        public void SetMap(int x, int y, elements e) 
        {
            if ((x <= 2 && x >= 0) && (y >= 0 && y <= 2))
            {       
                map[x, y] = e;
                        
            }
            
        }

        public elements GetMap(int x, int y )
        {
            if ((x <= 2 && x >= 0) && (y >= 0 && y <= 2))
            {
                return map[x, y];
            }

            return elements.outSideMap;
            
        }

        public void ShowMap() 
        {
            ConsoleColor old = Console.ForegroundColor; 
            Console.ForegroundColor = ConsoleColor.Green;

            Console.WriteLine("{0} {1} {2}", map[0, 0], map[1, 0], map[2, 0]);
            Console.WriteLine("{0} {1} {2}", map[0, 1], map[1, 1], map[2, 1]);
            Console.WriteLine("{0} {1} {2}", map[0, 2], map[1, 2], map[2, 2]);

            Console.ForegroundColor = old;


        
        }

        public bool LogicUpdate() 
        {
            p.SetElementOnMap();
            c.SetElementOnMap();

            //********* o check
            elements SelectedForCheck = p.MapBrush;

            //1 line
            if ((SelectedForCheck == map[0, 0]) && (SelectedForCheck == map[0, 1]) && (SelectedForCheck == map[0, 2])) 
            {
                p_win = true;
            }
            //2 line
            if ((SelectedForCheck == map[1, 0]) && (SelectedForCheck == map[1, 1]) && (SelectedForCheck == map[1, 2]))
            {
                p_win = true;
            }

            //3 line
            if ((SelectedForCheck == map[2, 0]) && (SelectedForCheck == map[2, 1]) && (SelectedForCheck == map[2, 2]))
            {
                p_win = true;
            }

            //1 line up
            if ((SelectedForCheck == map[0, 0]) && (SelectedForCheck == map[1, 0]) && (SelectedForCheck == map[2, 0]))
            {
                p_win = true;
            }
            //2 line up
            if ((SelectedForCheck == map[0, 1]) && (SelectedForCheck == map[1, 1]) && (SelectedForCheck == map[2, 1]))
            {
                p_win = true;
            }

            //3 line up
            if ((SelectedForCheck == map[0, 2]) && (SelectedForCheck == map[1, 2]) && (SelectedForCheck == map[2, 2]))
            {
                p_win = true;
            }

            // krest 1
            if ((SelectedForCheck == map[0, 0]) && (SelectedForCheck == map[1, 1]) && (SelectedForCheck == map[2, 2]))
            {
                p_win = true;
            }

            // krest 1
            if ((SelectedForCheck == map[2, 0]) && (SelectedForCheck == map[1, 1]) && (SelectedForCheck == map[0, 2]))
            {
                p_win = true;
            }


            //***********x check
            SelectedForCheck = c.MapBrush;

            //1 line
            if ((SelectedForCheck == map[0, 0]) && (SelectedForCheck == map[0, 1]) && (SelectedForCheck == map[0, 2]))
            {
                c_win = true;
            }
            //2 line
            if ((SelectedForCheck == map[1, 0]) && (SelectedForCheck == map[1, 1]) && (SelectedForCheck == map[1, 2]))
            {
                c_win = true;
            }

            //3 line
            if ((SelectedForCheck == map[2, 0]) && (SelectedForCheck == map[2, 1]) && (SelectedForCheck == map[2, 2]))
            {
                c_win = true;
            }

            //1 line up
            if ((SelectedForCheck == map[0, 0]) && (SelectedForCheck == map[1, 0]) && (SelectedForCheck == map[2, 0]))
            {
                c_win = true;
            }
            //2 line up
            if ((SelectedForCheck == map[0, 1]) && (SelectedForCheck == map[1, 1]) && (SelectedForCheck == map[2, 1]))
            {
                c_win = true;
            }

            //3 line up
            if ((SelectedForCheck == map[0, 2]) && (SelectedForCheck == map[1, 2]) && (SelectedForCheck == map[2, 2]))
            {
                c_win = true;
            }

            // krest 1
            if ((SelectedForCheck == map[0, 0]) && (SelectedForCheck == map[1, 1]) && (SelectedForCheck == map[2, 2]))
            {
                c_win = true;
            }

            // krest 1
            if ((SelectedForCheck == map[2, 0]) && (SelectedForCheck == map[1, 1]) && (SelectedForCheck == map[0, 2]))
            {
                c_win = true;
            }


            // ret
            if (p_win == c_win) 
                return true;
            else
                return false;
        }

        



    }
}
